package stream.method;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @CreateBy zhangdingyi
 * @Date 2022-10-12
 * @Content 1.9 收集到映射表中 程序清单
 */
public class Six {
    public static void main(String[] args) {
        // 设置值的方法 与 函数表达
        //第二个参数 可以设置成 方法 或者 对象
        //Map<Integer, String> stringMap = getPerson().collect(Collectors.toMap(Person::getId, Person::getName));
        //System.out.println("stringMap -> " + stringMap);
        //Map<Integer, Person> stringMap2 = getPerson().collect(Collectors.toMap(Person::getId, Function.identity()));
        //System.out.println("stringMap2 -> " + stringMap2.getClass().getName() + " -> " + stringMap2);

        //获取 treeMap
        //stringMap2 = getPerson().collect(Collectors.toMap(Person::getId, Function.identity(), (v1, v2) -> v1, TreeMap::new));
        //System.out.println("treeMap stringMap2 -> " + stringMap2.getClass().getName() + " -> " + stringMap2);

        // toMap 重复数据处理 --把数据变成 Map<String, Set<String>>
        Stream<Locale> localeStream = Stream.of(Locale.getAvailableLocales());
        Map<String, Set<String>> stringSetMap = localeStream.collect(
                Collectors.toMap(Locale::getDisplayCountry,
                        l -> Collections.singleton(l.getDisplayCountry()), //这个是固定的模式
                        (v1, v2) -> {
                            //Set<String> unions = new HashSet<>(v1);
                            Set<String> unions = new HashSet<>(v1);
                            //System.out.println("v1 -> " + v1);
                            //System.out.println("v2 -> " + v2);
                            unions.addAll(v2);
                            System.out.println(" unions -> " + unions);
                            //这个
                            return unions;
                        }
                ));
        //数据显示的模式为 {泰国=[泰国], 巴西=[巴西]} ,[巴西]这个是 Set<String>
        System.out.println("stringSetMap-> " + stringSetMap.getClass().getName() + " -> " + stringSetMap);
        Set<String> strings = stringSetMap.get("泰国");
        System.out.println("strings -> " + strings);

        // toMap 处理重复数据 且 id 相同把 数据整合
        Map<Integer, Person> integerPersonMap = getPerson().collect(Collectors.toMap(Person::getId, Function.identity(), (v1, v2) -> {
            v1.setName(v1.getName() + "-" + v2.getName());
            return v1;
        }));
        System.out.println("integerPersonMap -> " + integerPersonMap);
    }

    public static Stream<Person> getPerson() {
        return Stream.of(new Person("one", 1),
                new Person("two", 2),
                new Person("three", 3),
                new Person("four", 3),
                new Person("five", 3));
    }

    public static class Person {
        String name;
        int id;

        public Person() {
        }

        public Person(String name, int id) {
            this.name = name;
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "name='" + name + '\'' +
                    ", id=" + id +
                    '}';
        }
    }


}
